﻿using System;
using System.Windows.Forms;
using Alerter.DataSet;
using DevExpress.Utils;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid.Views.Grid;

namespace Alerter.Controls
{
    public partial class ContactListUserControl : UserControl, IList, IGridView
    {
        private Controller _controller;

        #region Properties

        public Controller Controller
        {
            set
            {
                _controller = value;
                _controller.ContactSaved += delegate { SetGridDataSource(); };
                SetGridDataSource();
            }
        }

        #endregion

        public ContactListUserControl()
        {
            InitializeComponent();
            gridView.DoubleClick += GridViewDoubleClick;
        }
        
        #region Private Methods

        private void SetGridDataSource()
        {
            ContactoDataSet dataSet = _controller.GetContacts();
            gridControl.DataSource = null;
            gridControl.DataSource = dataSet;
            gridControl.DataMember = dataSet.Contacto.TableName;
            SetupOperatorLookupData();            
        }

        private void ShowContact(long documentId)
        {
            var window = new ContactXtraForm { DocumentId = documentId, Controller = _controller };
            window.ShowDialog();
        }

        private void SetupOperatorLookupData()
        {
            operadoraDataSet.Merge(_controller.GetOperators());
            operatorRepositoryItemLookUpEdit.Columns.Clear();
            operatorRepositoryItemLookUpEdit.Columns.AddRange(new[] {
                new LookUpColumnInfo("Id", "Id", 81, FormatType.None, "", false, HorzAlignment.Near),
                new LookUpColumnInfo("Nome", "Nome", 85, FormatType.None, "", true, HorzAlignment.Near)});
            operatorRepositoryItemLookUpEdit.DataSource = operadoraDataSet.Operadora;
            operatorRepositoryItemLookUpEdit.DisplayMember = operadoraDataSet.Operadora.NomeColumn.ColumnName;
            operatorRepositoryItemLookUpEdit.ValueMember = operadoraDataSet.Operadora.IdColumn.ColumnName;
        }

        #endregion

        public void AddDocument()
        {
            ShowContact(-1);
        }

        public void DeleteDocument()
        {
            var document = (ContactoDataSet.ContactoRow)gridView.GetDataRow(gridView.FocusedRowHandle);
            if (document == null)
                return;
            var message = string.Format("'{0}' será eliminado definitivamente.", document.Nome);
            if (_controller.ContactHasRelations(document.Id))
                message += Environment.NewLine + "O contacto está associado a mensagens e/ou grupos.";
            var result = MessageBox.Show(_controller.OwnerWindow, message, "Contacto", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);
            if (result != DialogResult.OK)
                return;
            _controller.DeleteContact(document.Id);
        }

        void GridViewDoubleClick(object sender, EventArgs e)
        {
            var contact = (ContactoDataSet.ContactoRow)gridView.GetDataRow(gridView.FocusedRowHandle);
            if (contact == null)
                return;
            ShowContact(contact.Id);
        }

        public GridView GridView()
        {
            return gridView;
        }

        public string ListName
        {
            get { return "ContactListUserControl"; }
        }

    }
}
